<?php
/* OpenBIBLIO -- A library administration web interface
 * Copyright (C) 2002-2005 Fr&eacute;d&eacute;ric Descamps <lefred@tiscali.be>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

if($_GET['id']) $id=$_GET['id'];
else $id=$_POST['id'];
$classe_q=$_GET['classe_q'];

if(!$id)
{       if(!($classe_q)) 
        {
                $requete = "select * from classes order by classe desc";
                if(!($resultat = biblio_db_query($requete)))
                { print "PROBLEME : " .biblio_db_error(); }
                else 
                {       while ($row = biblio_db_fetch_array($resultat)) 
                        {
                          $classe_q = $row['id'];
                        }
                }
        }        
        $requete = "select t1.id, t1.nom, t1.prenom, t1.classe_id, t2.classe from eleves as t1 inner join classes as t2 on t2.id = t1.classe_id where t1.classe_id=$classe_q and t1.del = 0 order by classe, nom, prenom ";
        if(!($resultat = biblio_db_query($requete)))
        { print "PROBLEME : " .biblio_db_error(); }
        else 
        {
                echo "<CENTER><H3>";
		echo _("Modifier un &eacute;l&egrave;ve");
		echo "</H3>";
                
                $requete2 = "select * from classes order by classe";
                if(!($resultat2 = biblio_db_query($requete2)))
                { print "PROBLEME : " .biblio_db_error(); }
                else 
                {
                  while ($row2 = biblio_db_fetch_array($resultat2)) 
	          {	          
	           echo " <A HREF=\"admin.php?action=mod&type=eleve&classe_q=". $row2['id'] . "\">" . $row2['classe'] . "</A> ";
	          }
                }       
                
                echo "<BR><BR><TABLE><TR><th>";
		echo _("Nom");
		echo "</th><th>";
		echo _("Pr&eacute;nom");
		echo "</th><th>";
		echo _("Classe");
		echo "</th><th>";
		echo _("Action");
		echo "</th>";
        
                while ($row = biblio_db_fetch_array($resultat)) 
	        {      
                        $eleve_id = $row['id'];           
                        $eleve_nom =  $row['nom'];                                
                        $eleve_prenom =  $row['prenom'];                                
                        $eleve_classe =  $row['classe'];                                
                        $eleve_classe_id =  $row['classe_id'];                                
                        echo "<TR><TD>$eleve_nom</TD><TD>$eleve_prenom</TD><TD>$eleve_classe</TD><TD ALIGN=RIGHT><A href=\"admin.php?action=mod&type=eleve&id=$eleve_id&nom=$eleve_nom&prenom=$eleve_prenom&classe_id=$eleve_classe_id\"><IMG SRC=\"images/wh_r_arr.gif\" ALT=\"";
			echo _("changer");
			echo "\" border=0></A></TD>";            
                }
                echo "</TABLE></CENTER>";
        }
}
else
{
    if ($_POST['sw']!=1)
    {
	$photo_path=get_picspath();
        ?>
        <CENTER><H3>
	<?php echo _("Modifier l'&eacute;l&egrave;ve"); ?>
	</H3>
	<img src=images/photos/<?php echo $id.".jpg"; ?> align=right>
        <FORM enctype=multipart/form-data  action=admin.php METHOD=POST>
        <TABLE BORDER=0>
        <TR>
        <TD>
	<?php echo _("Nom"); ?> 
	:</TD><TD><INPUT TYPE="text" NAME="el_nom" size=15 value="<?php echo $_GET['nom']; ?>" ></TD>
        <TR>
        <TD>
	<?php echo _("Pr&eacute;nom"); ?>
	:</TD><TD><INPUT TYPE="text" NAME="el_prenom" size=15 value="<?php echo $_GET['prenom']; ?>"></TD>
        <TR>
        <TD>
	<?php echo _("Classe"); ?>
	:</TD><TD><select NAME="el_classe_id">
        <?php
        $requete = "SELECT * FROM classes order by classe"; 
	if(!($resultat = biblio_db_query($requete)))
	{ print "PROBLEME : " .biblio_db_error(); }
	else 
	{
		while ($row = biblio_db_fetch_array($resultat)) 
		{      
		        $el_classe_id = $row['id'];           
			$el_classe_l =  $row['classe'];                                
			if ($classe_id == $el_classe_id) { $str = "selected";}
			else { $str = ""; }
			print "<option VALUE=\"$el_classe_id\" $str>$el_classe_l";
		}
	}                                
                                
        ?>
        </select>
        </TD>
	</tr>
	<tr>
 	<TD>
	<?php echo _("Photo"); ?>
	:</TD><TD><input type=file name="el_photo"></TD></TR>
        </TABLE><BR>
        <INPUT TYPE="hidden" NAME="user" value="<?php echo $user; ?>">
        <INPUT TYPE="hidden" NAME="action" value="<?php echo $action; ?>">
        <INPUT TYPE="hidden" NAME="type" value="<?php echo $type; ?>">
        <INPUT TYPE="hidden" NAME="sw" value="1">
        <INPUT TYPE="hidden" NAME="id" value="<?php echo $id; ?>">        
        <input type="Submit" value="Enregistrer">
        </FORM>
        </CENTER>        
        <BR><BR><A HREF="printeleve.php?eleve_id=<?php echo $id; ?>" target="NEW">
	<?php echo _("Imprimer la carte de cet utilisateur"); ?>
	</A>
        <?php
    }   
    else
    {          
      $el_nom=$_POST['el_nom'];
      $el_prenom=$_POST['el_prenom'];
      $el_classe_id=$_POST['el_classe_id'];
      $id=$_POST['id'];
      $user=$_POST['user'];
      if ($_FILES['el_photo']['tmp_name'])
      {
	$filename = $id . ".jpg";
	$photo_path=get_picspath();
	system("cp ". $_FILES['el_photo']['tmp_name'] ." $photo_path/$filename");
        system("cd $photo_path; mogrify -geometry 220x340 $filename");
      }
      $requete = "update eleves set nom='$el_nom', prenom='$el_prenom',classe_id='$el_classe_id' where id=$id";
      if(!($resultat = biblio_db_query($requete)))
      { echo "PROBLEME : " .biblio_db_error(); exit;}  
      put_log(3,$requete,$user);
      echo "<CENTER><BR>";
      echo _("L'&eacute;l&egrave;ve a &eacute;t&eacute; modifi&eacute;.");
      echo "<br></center>";
    } 
}

?>
<br><br>
<div>
<span id=left-text>
<A HREF="admin.php?menu=1">
<?php echo _("retour menu administration"); ?>
</A><BR>
<A HREF="index.php">
<?php echo _("retour menu principal"); ?>
</A></span>
</div>
<br>
